package CtrLayer;
import ModLayer.*;
import java.util.ArrayList;

/**
 * Write a description of class LeaseCtr here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class LeaseCtr
{
    private LeaseCon leasecon;
    private PersonCtr personctr;
    private ProductCtr productctr;
    private int location;
    private TimeAndCheck time;
    private Company company;
    public static LeaseCtr instance;
    
    private LeaseCtr()
    {
       leasecon = leasecon.getInstance();
       time = time.getInstance();
       personctr = personctr.getInstance();
    }
    
    public static LeaseCtr getInstance()
    {
        if(instance==null)
            instance = new LeaseCtr();
        return instance;
    }
    
    public void setLocation(String location)
    {
        if(location == "DIY")
            this.location = 1;
        else if(location == "Bathroom")
            this.location = 2;
        else
            this.location = 3;
    }
    
    public void createLease(String idP, String idC, String idE, String startD, int dur)
    {
        if(personctr.searchPerson(idE, "employee") != null &&
        (personctr.searchPerson(idC, "employee") != null || true))
        //the true value is a placeholder for a method that would verify an unregistered customer's bank code
            leasecon.createLease(idP, time.getNoForCustomer(), idC, idE, startD, dur);
    }
    
    public ArrayList<Lease> searchLease()
    {
        return leasecon.searchLease();
    }
    
    public void removeLease(String id)
    {
        leasecon.removeLease(id);
    }
    
    public Lease findLease(String id)
    {
        return leasecon.findLease(id);
    }
    
    public boolean isCustomerRegistered(Lease s)
    {
        PersonInformations cobj = personctr.searchPerson(s.getIDCustomer(), "employee");
        Customer c = (Customer) cobj;
        if(c == null)
            return false;
        return true;
    }
    
    public void checkout(String id, double discount)
    {
        Lease l = findLease(id);
        Product p = productctr.findProduct(l.getIDProduct());
        double sum = p.getLeasePrice(p.getDiscount())*l.getDuration();
        sum = sum/100*(100-discount);
        PersonInformations cobj = personctr.searchPerson(l.getIDCustomer(), "employee");
        PersonInformations eobj = personctr.searchPerson(l.getIDEmployee(), "employee");
        Customer c = (Customer) cobj;
        Employee e = (Employee) eobj;
        if(c != null)
        {
            double total = Double.parseDouble(c.getOwe()+sum);
            c.setOwe(String.valueOf(total));
        }
        double total = Double.parseDouble(e.getTurnover()+sum);
        e.setTurnover(String.valueOf(total));
    }
}